﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CardCharger.lib;
namespace CardCharger.forms
{
    public partial class CardRecharge : Form
    {
        CardOS cos;
        UserCardIssue uci;

        public CardRecharge(CardOS _cos)
        {
            InitializeComponent();
            cos = _cos;
            uci = new UserCardIssue(cos);
            uci.Rechargeloghandler += uci_Rechargeloghandler;
            
        }

        void uci_Rechargeloghandler(object sender, string msgType, string msgLog, string options)
        {
            if (this.rtb_view.InvokeRequired)
            {
                Ulitily.LogEventHandler h = new Ulitily.LogEventHandler(uci_Rechargeloghandler);
                this.rtb_view.BeginInvoke(h, new object[] { 
                sender,msgType,msgLog,options});
            }
            else
            {
                this.rtb_view.AppendText(msgType + ":" + msgLog + "\r\n");
            }
        }

        private void button_readCard_Click(object sender, EventArgs e)
        {
            if (uci.ReadShouDian() == false) this.rtb_view.AppendText("\r\n读卡失败.\r\n");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.rtb_view.Clear();
        }

        private void button_Charge_Click(object sender, EventArgs e)
        {
            
            try {
                double m = double.Parse(this.textBox_Value.Text, System.Globalization.NumberStyles.Float);
                bool dec = false;
                this.textBox_Value.Text = string.Format("{0:0.00}",m);

                if (m < 0) { dec = true; m *= -1; }

                UInt32 mm = (UInt32)(m * 100);

                uci.ChargeValue(mm, dec);

                

            }
            catch
            {


            }
        }

        private void B_Lock_Click(object sender, EventArgs e)
        {
            try {
                uci.LockCard(true);
            }
            catch { }
        }

        private void B_UnLock_Click(object sender, EventArgs e)
        {
            try {
                uci.LockCard(false);
            }
            catch { }
        }
    }
}
